package com.xtoneict;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.ScriptableObject;

import javax.script.*;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;

/**
 * <pre>
 * @TODO 描述文件用途
 * </pre>
 *
 * @author jew
 * @date 2024/3/13 14:52
 * @company 成都信通信息技术有限公司
 */
public class JSUtils {

    private static final String BASE = "D:\\work\\IdeaWorkSpace\\singlewindow\\src\\main\\resources\\js\\";


    private static ScriptEngine engine1;

    static {
        try {
            ScriptEngineManager manager = new ScriptEngineManager();
            engine1 = manager.getEngineByName("javascript");
            Bindings engineScope = engine1.getBindings(ScriptContext.ENGINE_SCOPE);
            engineScope.put("window", engineScope);
            engineScope.put("navigator", engineScope);
            InputStreamReader jsencryptFileReader = new FileReader(BASE + "encrypt1.js");
            engine1.eval(jsencryptFileReader);
            jsencryptFileReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * <pre>
     *     字符串加密，用户登录时使用
     * </pre>
     *
     * @param value
     * @param key
     * @return
     */
    public static String CaData_KXC(String value, String key) {
        // 创建执行上下文
        Context context = Context.enter();
        context.setOptimizationLevel(-1);
        try {
            // 创建全局作用域
            ScriptableObject scope = context.initStandardObjects();

            // 编译JavaScript代码
            Script script = context.compileReader(new FileReader(BASE + "encrypt.js"), "script", 1, null);
            //Script script1 = context.compileReader(new FileReader("C:\\Users\\62772\\Desktop\\test.js"), "script1", 1, null);

            // 执行JavaScript代码
            Object result = script.exec(context, scope);
            //Object result1 = script1.exec(context, scope);

            // 调用JavaScript方法
            Object result1 = context.evaluateString(scope, "window.CaData_KXC('" + value + "', '" + key + "');", "<cmd>", 1, null);
            return result1.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            // 退出执行上下文
            Context.exit();
        }
    }


    public static String loadMenuTypeStr(String value) {
        // 创建执行上下文
        Context context = Context.enter();
        context.setOptimizationLevel(-1);
        try {
            // 创建全局作用域
            ScriptableObject scope = context.initStandardObjects();

            // 编译JavaScript代码
            Script script = context.compileReader(new FileReader(BASE + "encrypt.js"), "script", 1, null);
            //Script script1 = context.compileReader(new FileReader("C:\\Users\\62772\\Desktop\\test.js"), "script1", 1, null);

            // 执行JavaScript代码
            Object result = script.exec(context, scope);
            //Object result1 = script1.exec(context, scope);

            // 调用JavaScript方法
            Object result1 = context.evaluateString(scope, "loadMenuTypeStr('" + value + "');", "<cmd>", 1, null);
            return result1.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            // 退出执行上下文
            Context.exit();
        }
    }

    public static String loadAESDecryptStr(String value){
        Invocable invoke = (Invocable) engine1;
        try {
            String result = (String) invoke.invokeFunction("loadAESDecryptStr", value);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public static void main(String[] args) throws Exception{
        System.out.println(loadAESDecryptStr("x1HPiJT9rX+PqIdXyN4oxAaHepMZXjR8aTFoCNOVTY0="));
        //System.out.println(test("x1HPiJT9rX+PqIdXyN4oxAaHepMZXjR8aTFoCNOVTY0="));
    }
}
